package com.octopus.samples.xici;

import com.octopus.core.Request;
import com.octopus.core.Response;
import com.octopus.core.parser.HtmlParser;
import com.octopus.core.parser.ParseException;
import com.octopus.core.parser.ParseResult;
import com.octopus.core.result.Result;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 * 解析代理服务器列表页面
 *
 * @author shoulai.yang@gmail.com
 * @date 2019/08/06
 */
public class XiciProxyParser extends HtmlParser {

  @Override
  public ParseResult parse(Document document, Response response) throws ParseException {
    ParseResult result = ParseResult.me();
    int page = response.request().extras().getInteger("page");
    if (page == 1) {
      // 第一页需要解析所有列表页面
      Element last =
          document
              .select(".pagination > a:not([class=previous_page]):not([class=next_page])")
              .last();
      if (last != null) {
        int lastPage = Integer.valueOf(last.text().trim());
        if (lastPage > 1) {
          for (int i = 2; i < lastPage; i++) {
            result.requests(Request.me("https://www.xicidaili.com/nn/" + i).extra("page", i));
          }
        }
      }
    }
    List<XiciProxy> proxies = new ArrayList<>();
    document
        .select("table#ip_list")
        .first()
        .select("tr:not(:first-of-type)")
        .forEach(
            tr -> {
              Elements tds = tr.select("td");
              XiciProxy proxy = new XiciProxy();
              proxy.setCountry(
                  tds.get(0).select("img").first() == null
                      ? null
                      : tds.get(0).select("img").first().attr("alt").toLowerCase());
              proxy.setHost(tds.get(1).text());
              proxy.setPort(Integer.valueOf(tds.get(2).text()));
              proxy.setAddress(
                  tds.get(3).select("a").first() == null
                      ? null
                      : tds.get(3).select("a").first().text());
              proxy.setType(tds.get(5).text());
              proxy.setSpeed(tds.get(6).select(">div").first().attr("title"));
              proxy.setConnectTime(tds.get(7).select(">div").first().attr("title"));
              proxy.setSurvivalTime(tds.get(8).text());
              proxy.setVerifyDate(tds.get(9).text());
              proxies.add(proxy);
            });
    result.body(Result.me().obj(proxies));
    return result;
  }
}
